﻿
// FileView.cpp: CFileView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "File.h"
#endif

#include "FileDoc.h"
#include "FileView.h"
#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CFileView

IMPLEMENT_DYNCREATE(CFileView, CView)

BEGIN_MESSAGE_MAP(CFileView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
	ON_COMMAND(IDM_FILE_WRITE, &CFileView::OnFileWrite)
	ON_COMMAND(IDM_FILE_READ, &CFileView::OnFileRead)
	ON_COMMAND(IDM_REG_WRITE, &CFileView::OnRegWrite)
	ON_COMMAND(IDM_REG_READ, &CFileView::OnRegRead)
END_MESSAGE_MAP()

// CFileView 构造/析构

CFileView::CFileView() noexcept
{
	// TODO: 在此处添加构造代码

}

CFileView::~CFileView()
{
}

BOOL CFileView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CFileView 绘图

void CFileView::OnDraw(CDC* /*pDC*/)
{
	CFileDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
}


// CFileView 打印

BOOL CFileView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CFileView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CFileView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CFileView 诊断

#ifdef _DEBUG
void CFileView::AssertValid() const
{
	CView::AssertValid();
}

void CFileView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CFileDoc* CFileView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFileDoc)));
	return (CFileDoc*)m_pDocument;
}
#endif //_DEBUG


// CFileView 消息处理程序


void CFileView::OnFileWrite()
{
	// TODO: 在此添加命令处理程序代码
	// 写入文件的相关操作
	//FILE* pfile = fopen("1.txt", "w");
	//fwrite("www.baidu.com", 1, strlen("www.baidu.com"), pfile);
	//fseek(pfile, 0, SEEK_SET);
	//fwrite("ftp", 1, strlen("ftp"), pfile);
	//fwrite("www.baidu.com", 1, strlen("www.baidu.com"), pfile);
	//fclose(pfile);
	//fflush(pfile);

	//FILE* pFile = fopen("2.txt", "w");
	//char ch[3];
	//ch[0] = 'a';
	//ch[1] = 10;
	//ch[2] = 'b';
	//fwrite(ch, 1, 3, pFile);
	//fclose(pFile);


	/*
	* 这个例子是需要注意文件的读写时对于整数的处理
	FILE* pFile = fopen("3.txt", "w");
	int i = 98341;
	char ch[6];
	//ch[0] = 9 + 48;
	//ch[1] = 8 + 48;
	//ch[2] = 3 + 48;
	//ch[3] = 4 + 48;
	//ch[4] = 1 + 48;
	_itoa(i, ch, 10);
	fwrite(ch, 1, 5, pFile);
	//fwrite(ch, 1, 5, pFile);
	fclose(pFile);
	*/


	/*
	* C++中对文件的操作

	std::ofstream ofs("4.txt");
	ofs.write("www.baidu.com", strlen("www.baidu.com"));
	ofs.close();
	*/

	/*window API文件的处理方式
	HANDLE hFile;
	hFile = CreateFile("5.txt", GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
	DWORD dwWrites;
	WriteFile(hFile, "www.baidu.com", strlen("www.baidu.com"), &dwWrites, NULL);// 写入数据
	CloseHandle(hFile);
	*/


	/*
	* CFile读写文件操作
	CFile file("6.txt", CFile::modeCreate | CFile::modeWrite);
	file.Write("www.baidu.com", strlen("www.baidu.com"));
	file.Close();
	*/

	CFileDialog fileDlg(false);
	fileDlg.m_ofn.lpstrTitle = "我的文件对话框";
	fileDlg.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	fileDlg.m_ofn.lpstrDefExt = "txt";
	if (IDOK == fileDlg.DoModal()) {
		CFile file(fileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite);
		file.Write("www.baidu.com", strlen("www.baidu.com"));
		file.Close();
	}

}


void CFileView::OnFileRead()
{
	// TODO: 在此添加命令处理程序代码
	// 读取文件，固定的大小空间
	//FILE* pFile = fopen("1.txt", "r");
	//char ch[100];
	//memset(ch, 0, 100);
	//fread(ch, 1, 100, pFile);
	//MessageBox(ch);

	/*
	* 读取文件 根据文件大小来开辟内存空间
	char* pBuf;
	fseek(pFile, 0, SEEK_END);
	int len = ftell(pFile);
	pBuf = new char[len + 1];
	rewind(pFile);
	fread(pBuf, 1, len, pFile);
	pBuf[len] = 0;
	MessageBox(pBuf);

	fclose(pFile);
	*/

	//FILE* pFile = fopen("2.txt", "rb");//  需要注意的是文件的读写要对应，都要严格按照二进制的读取或者文本的方式读写
	//char ch[100];
	//fread(ch, 1, 3, pFile);
	//ch[3] = 0;
	//MessageBox(ch);
	//fclose(pFile);

	/*
	FILE* pFile = fopen("3.txt", "r");
	char ch[100];
	fread(ch, 1, 5, pFile);
	ch[5] = 0;
	MessageBox(ch);
	fclose(pFile);
	*/


	/*
	std::ifstream ifs("4.txt");
	char ch[100];
	memset(ch, 0, 100);
	ifs.read(ch, 100);
	ifs.close();
	MessageBox(ch);
	*/

	/*
	HANDLE hFile;
	hFile = CreateFile("5.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	char ch[100];
	DWORD dwReads;
	ReadFile(hFile, ch, 100, &dwReads, NULL);
	ch[dwReads] = 0;
	CloseHandle(hFile);
	MessageBox(ch);
	*/


	/*
	CFile file("6.txt", CFile::modeRead);
	char* pBuf;
	DWORD dwFileLen;
	dwFileLen = file.GetLength();
	pBuf = new char[dwFileLen + 1];
	pBuf[dwFileLen] = 0;
	file.Read(pBuf, dwFileLen);
	file.Close();
	MessageBox(pBuf);
	*/


	CFileDialog fileDlg(true);
	fileDlg.m_ofn.lpstrTitle = "我的打开文件对话框";
	fileDlg.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	if (IDOK == fileDlg.DoModal()) {
		CFile file(fileDlg.GetFileName(), CFile::modeRead);
		char* pBuf;
		DWORD dwFileLen;
		dwFileLen = file.GetLength();
		pBuf = new char[dwFileLen + 1];
		pBuf[dwFileLen] = 0;
		file.Read(pBuf, dwFileLen);
		file.Close();
		MessageBox(pBuf);
	}

}


void CFileView::OnRegWrite()
{
	// TODO: 在此添加命令处理程序代码
	HKEY hKey;
	DWORD dwAge = 30;
	RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\http://www.baidu.com\\admin", &hKey);
	RegSetValue(hKey, NULL, REG_SZ, "zhangsan", strlen("zhangsan"));
	RegSetValueEx(hKey, "age", 0, REG_DWORD, (const BYTE*)&dwAge, 4);
	RegCloseKey(hKey);
}


void CFileView::OnRegRead()
{
	// TODO: 在此添加命令处理程序代码
	LONG lValue = 0;
	RegQueryValue(HKEY_LOCAL_MACHINE, "Software\\http://www.baidu.com\\admin", NULL, &lValue);
	char* pBuf = new char[lValue];
	RegQueryValue(HKEY_LOCAL_MACHINE, "Software\\http://www.baidu.com\\admin", pBuf, &lValue);
	//MessageBox(pBuf);




	HKEY hKey;
	RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\http://www.baidu.com\\admin", &hKey);

	DWORD dwType;
	DWORD dwValue;
	DWORD dwAge;
	RegQueryValueEx(hKey, "age", 0, &dwType, (LPBYTE)&dwAge, &dwValue);
	CString str;
	str.Format("age=%d", dwAge);
	MessageBox(str);

}
